package com.sdhs.paas.nativedbdemo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sdhs.paas.sdk.core.PaasDAOService;
import com.sdhs.paas.sdk.core.jdbc.pool.PaasDS;
/**
 * 使用nativesdk数据源测试
 * */
@RestController
public class DemoDAOService extends PaasDAOService{
	@Autowired
	PaasDS ds;
	 @GetMapping("/dbtest")
	    public String findByIdUseDs() throws SQLException, JsonProcessingException {
	        Connection cc = null;
	        Map<String,Object> map = null;
	        try {
	            cc = ds.getConnection();
	            PreparedStatement pst = cc.prepareStatement("select * from uc_users where uid=1");
	            ResultSet rs = pst.executeQuery();
	            map = new HashMap<String,Object>();
	            while (rs.next()) {
	            	map.put("userId", rs.getString("user_id"));
	            	map.put("userName", rs.getString("user_name"));
	            }

	        } catch (SQLException e) {
	        } finally {
	            if (cc != null) {
	                cc.close();            //使用连接池，必须在finally里关闭连接
	            }
	        }
	        ObjectMapper mapper = new ObjectMapper();
	        String str = mapper.writeValueAsString(map);
	        return str;
	    }

}
